﻿/*-----------------------------------------------
// File: AttackState.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Battle.View
{

    public class AttackState :Fsm.FsmStateBase<BattleRoundState>
    {
        protected BattleRoundCtrl battleRoundCtrl = null;
        public AttackState(BattleRoundState state, BattleRoundCtrl ctrl) : base(state, ctrl) { battleRoundCtrl = ctrl; }

        public override void Enter()
        {
        }

        public override void Update()
        {
            while (IsHaveBehaviourNeedDo()) {
                var package = GetNeedDoPackage();
                var bc = BattleViewCtrl.GetInstance().GetBattleCharacter(package.actorUid);
                bc.AddPackage(package);
            }


            if (BattleViewCtrl.GetInstance().IsCurrentBehaviourDoOver() && IsAllAttackReadyState()) {
                battleRoundCtrl.SwitchState(BattleRoundState.RoundWaitState);
                return;
            }

            if (BattleViewCtrl.GetInstance().IsHaveUltimateBehaviour()) {
                ToUltimate();
                return;
            }

        }

        void ToUltimate() {
            battleRoundCtrl.SwitchState(BattleRoundState.UltimateBeforeState);
        }


        bool IsHaveBehaviourNeedDo() {
            return BattleViewCtrl.GetInstance().IsHaveBehaviourNeedDo();
        }
        bool IsAllAttackReadyState() {
            foreach (var uid in BattleViewCtrl.GetInstance().GetActorUids(battleRoundCtrl.currentCamp)) {
                var bc = BattleViewCtrl.GetInstance().GetBattleCharacter(uid);
                if (!bc.isAlive) {
                    continue;
                }
                if (bc != null && bc.GetCurrrentStateType() != ActorState.AttackReady)
                    return false;
            }
            return true;
        }
        OnceBehaviourPackage GetNeedDoPackage() {
            return BattleViewCtrl.GetInstance().DequeueBehaviour();
        }
        
    }
}
